﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YiCreateWebApp.DBConnect;
using System.Data;

namespace YiCreateWebApp.User
{
    public partial class Register : System.Web.UI.Page
    {
        int reValue;    //用于保存返回值，返回值为-1（用户名存在）、0（失败）、1（成功）、2（用户名不存在）
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnkbtnCheck_Click(object sender, EventArgs e)
        {
            //查找用户名是否存在，已经存在则返回-1，不存在则返回2
            reValue = CheckName();
            if (reValue == -1)
            {
                Response.Write("<script>alert('用户名存在！');</script>");
                this.txtUserName.Focus();
            }
            else if (reValue == -2)
            {
                Response.Write("<script>alert('恭喜您！该用户名尚未注册！');</script>");
                this.txtUserName.Focus();
            }
        }


        //验证用户名是否存在
        public int CheckName()
        {
            //实例化公共类对象
            DB db = new DB();
            //Sql语句统计这个UserName的个数，如果不为0，表示用户存在，否则不存在 
            string str = "select count(*) from tb_User where UserName='" + this.txtUserName.Text + "'";
            try
            {
                DataTable dt = db.reDt(str);
                if (dt.Rows[0][0].ToString() != "0")
                {
                    return -1;  //该用户已经存在
                }
                else
                {
                    return 2;   //该用户尚未注册
                }
            }
            catch (Exception e)
            {
                return 0;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (reValue == -1)
            {
                Response.Write("<script>alert('用户名存在！');</script>");

            }
            else
            {
                DB db = new DB();
                string UserName = this.txtUserName.Text;
                string PassWord = db.GetMD5(this.txtPwd.Text.ToString());
                string Email = this.txtEmail.Text;

                string cmdstr = "insert into tb_User(UserName,PassWord,Email,Role) values('" + UserName + "','" + PassWord + "','" + Email + "',0  )";
                try
                {
                    reValue = db.SqlEx(cmdstr);
                    if (reValue == 1)
                    {
                        Response.Write("<script>alert('注册成功！');</script>");
                        Clear();    //清空文本框
                    }
                    else if (reValue == 0)
                    {
                        Response.Write("<script>alert('注册失败！');</script>");
                    }
                }
                catch (Exception ee)
                {
                    Response.Write("<script>alert('注册失败！');</script>");
                }
            }
        }

        protected void Clear()
        {
            this.txtUserName.Text = "";
            this.txtPwd.Text = "";
            this.txtRePwd.Text = "";
            this.txtEmail.Text = ""; 
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/User/Login_div.aspx");
        }

    }
}